package com.ctgu.fcftsystem.controller;

import com.ctgu.fcftsystem.pojo.Sharesholder;
import com.ctgu.fcftsystem.pojo.Spend;
import com.ctgu.fcftsystem.service.SharesholderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/SharesholderController")
public class SharesholderController {

    @Autowired
    private SharesholderService sharesholderService;

    @RequestMapping("/findByPaging")
    public Object findByPaging(int pageCount,int pageSize){
        ModelMap map = new ModelMap();
        PageHelper.startPage(pageCount, pageSize);//设置数据库分页查询的范围
        try {
            List<Sharesholder> userList = sharesholderService.findByPaging(pageCount, pageSize);
            //分页的信息
            PageInfo<Sharesholder> pageInfo=new PageInfo<>(userList);
            map.put("msg","查询所有角色成功");
            map.put("data",pageInfo);
            map.put("code",0);
        }catch (Exception e){
            e.getStackTrace();
            map.put("msg","查询所有角色失败");
            map.put("code",1);
        }
        return map;
    }

    /**
     * 添加持股信息
     * @param noteTaker
     * @param sharesName
     * @param securitiesId
     * @param holder
     * @param createTime
     * @param updateTime
     * @return
     */
    @RequestMapping("/addSharesholderDao")
    public Map addSharesholderDao(String noteTaker, String sharesName, Integer securitiesId
        , String holder, String createTime, String updateTime){
            Map<String,Object> map = new HashMap<>();
            int flag;
            if(noteTaker!=null&&sharesName!=null&&(securitiesId!=null&&securitiesId>0)&&holder!=null&&createTime!=null
            &&updateTime!=null){
                Sharesholder sharesholder = new Sharesholder();
                sharesholder.setNoteTaker(noteTaker);
                sharesholder.setSharesName(sharesName);
                sharesholder.setSecuritiesId(securitiesId);
                sharesholder.setHolder(holder);
                sharesholder.setCreateTime(createTime);
                sharesholder.setUpdateTime(updateTime);
                flag = sharesholderService.addSharesholderDao(sharesholder);
            }else {
                flag=-1;
            }


            if(flag>0){
                    map.put("code",0);
                    map.put("msg","持股信息添加成功");
                    map.put("data",flag);
                }else{
                    map.put("code",1);
                    map.put("msg","持股信息添加失败");
                    map.put("data",-1);

                }
                return  map;


        }

        /**
         * 根据股票名称、holder和证券用户等多重条件查询持股信息
         * @param sharesName
         * @param holder
         * @param securitiesId
         * @return
         */
        @RequestMapping("/searchSharesholder")
        public  Map searchSharesholder(String sharesName, String holder, Integer securitiesId){
            Map<String, Object> map = new HashMap<>();

            List<Sharesholder> sharesholderList = sharesholderService.searchSharesholder(sharesName,holder,securitiesId);
            if(sharesholderList!=null&&sharesholderList.size()>0){
                map.put("code",0);
                map.put("msg","查询成功");
                map.put("data",sharesholderList);
            }else{
                map.put("code",1);
                map.put("msg","查询失败");
                map.put("data",-1);
            }

            return map;
        }


    /**
     * 根据id删除持股信息
     * @param id
     * @return
     */
    @RequestMapping("/deleteSharesholderById")
    public Map deleteSharesholderById(Integer id){
        Map<String, Object> map = new HashMap<>();
        int flag = sharesholderService.deleteSharesholderById(id);

        if(flag==1){
            map.put("code",0);
            map.put("msg","删除持股信息成功");
            map.put("data",flag);
        }
        else{
            map.put("code",1);
            map.put("msg","删除持股信息失败");
            map.put("data",-1);
        }
        return map;
    }

    /**
     * 根据id维护持股信息
     * @param id
     * @param noteTaker
     * @param sharesName
     * @param securitiesId
     * @param holder
     * @param createTime
     * @param updateTime
     * @return
     */
    @RequestMapping("/updateSharesholderById")
    public Map updateSharesholderById(Integer id, String noteTaker, String sharesName, Integer securitiesId
            , String holder, String createTime, String updateTime){



        Map<String, Object> map = new HashMap<>();
        Sharesholder sharesholder = new Sharesholder();
        int flag;
        if((id!=null&&id>0)&&(noteTaker!=null||sharesName!=null||(securitiesId!=null&&securitiesId>0)||holder!=null||createTime!=null
                ||updateTime!=null)){
        sharesholder.setId(id);
        sharesholder.setNoteTaker(noteTaker);
        sharesholder.setSharesName(sharesName);
        sharesholder.setSecuritiesId(securitiesId);
        sharesholder.setHolder(holder);
        sharesholder.setCreateTime(createTime);
        sharesholder.setUpdateTime(updateTime);

         flag = sharesholderService.updateSharesholderById(sharesholder);}
        else {
            flag=-1;
        }


        if(flag>0){
            map.put("code",0);
            map.put("msg","维护持股信息成功");
            map.put("data",flag);
        }
        else{
            map.put("code",1);
            map.put("msg","维护持股信息失败");
            map.put("data",flag);
        }
        return map;
    }
}
